package com.intexh.quickandroid.module.chat.ui.face;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.intexh.quickandroid.R;


/**
 * Created by engine on 14-1-6.
 */
public class FaceView extends RelativeLayout implements FacePageAdapter.FaceItemClickListener, EmojiPageAdapter.EmojiClickListener {
    ViewPager viewPager;
    private Context mContext;
    private OnFaceSelect onFaceSelect;
    private FacePageAdapter facePageAdapter;
    private EmojiPageAdapter emojiPageAdapter;
    private LinearLayout mItemLayout;
    private MODE currentMode = MODE.FACE;
    private ViewPager.OnPageChangeListener onPageChangeListener;
    private static final String TAG = "FaceView";

    public enum MODE {
        FACE, EMOJI
    }

    public void setCurrentMode(MODE currentMode) {
        this.currentMode = currentMode;
    }

    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }

    public static enum FaceType {
        YOLKS, EMOJI
    }

    public static interface OnFaceSelect {
        public void onFaceChoice(Face face);

        public void onEmojiClick(String code);
    }

    public void setOnFaceSelect(OnFaceSelect onFaceSelect) {
        this.onFaceSelect = onFaceSelect;
    }

    public FaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        LayoutInflater.from(context).inflate(R.layout.face_layout, this);
        viewPager = (ViewPager) findViewById(R.id.pageView);
        viewPager.setOffscreenPageLimit(0);
        mItemLayout = (LinearLayout) findViewById(R.id.showItme);
    }

    public FaceView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

	}

	public FaceView(Context context) {
		super(context);

	}

	public void setFaceType(FaceType type, FragmentManager fragmentManager) {
        if (type == FaceType.YOLKS) {
            viewPager.setAdapter(facePageAdapter);
        } else {
            if (emojiPageAdapter == null) {
                emojiPageAdapter = new EmojiPageAdapter(getContext(), this);
            }

            viewPager.setAdapter(emojiPageAdapter);
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView();
    }

    private void initView() {
        facePageAdapter = new FacePageAdapter(mContext, this);
        viewPager.setAdapter(facePageAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (onPageChangeListener != null)
                    onPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                resetPages();
                ImageView imageView = (ImageView) mItemLayout.getChildAt(position);
                if (imageView != null) {
                    imageView.setImageResource(R.mipmap.page_indicat_on);
                }
                if (onPageChangeListener != null)
                    onPageChangeListener.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (onPageChangeListener != null)
                    onPageChangeListener.onPageScrollStateChanged(state);
            }
        });
        resetPages();
    }

    public void resetPages() {
        mItemLayout.removeAllViews();
        int mar = mContext.getResources().getDimensionPixelSize(R.dimen.indicat_marg);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(mar, 0, 0, 0);
        if (currentMode == MODE.FACE) {
            for (int i = 0; i < facePageAdapter.getCount(); i++) {
                ImageView imageView = new ImageView(mContext);
                imageView.setImageResource(R.mipmap.page_indicat);
                imageView.setLayoutParams(layoutParams);
                mItemLayout.addView(imageView);
            }
        } else {
            for (int i = 0; i < emojiPageAdapter.getCount(); i++) {
                ImageView imageView = new ImageView(mContext);
                imageView.setImageResource(R.mipmap.page_indicat);
                imageView.setLayoutParams(layoutParams);
                mItemLayout.addView(imageView);
            }

        }
    }

    public void setOnPageSelectListener(ViewPager.OnPageChangeListener onPageSelectListener) {
        this.onPageChangeListener = onPageSelectListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    @Override
    public void onFaceItemClick(Face item) {
        onFaceSelect.onFaceChoice(item);
    }

    @Override
    public void onEmojiClick(String emojiCode) {
        onFaceSelect.onEmojiClick(emojiCode);
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            if (onFaceSelect != null) {
                Object item = adapterView.getAdapter().getItem(i);
                if (item instanceof Face)
                    onFaceSelect.onFaceChoice((Face) item);
                else
                    onFaceSelect.onEmojiClick((String) item);
            }
        }
    };

    public void setOnYolksClickListener(OnClickListener onYolksClickListener) {
        findViewById(R.id.yolks).setOnClickListener(onYolksClickListener);
    }

    public void setOnEmojiClickListener(OnClickListener onEmojiClickListener) {
        findViewById(R.id.emoji).setOnClickListener(onEmojiClickListener);
    }
}
